package org.ymh.unix_file_system.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.ymh.unix_file_system.domain.DirNode;
import org.ymh.unix_file_system.domain.GlobalVariable;
import org.ymh.unix_file_system.domain.INode;
import org.ymh.unix_file_system.domain.VisitingRecordItem;

import java.util.Stack;

/**
 * @author lenovo
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 9:42
 */
@Repository
public class VisitingRecordOperate {
    @Autowired
    private JsonDataOperate jsonDataOperate;
    @Autowired
    private DirNodeOperate dirNodeOperate;
    @Autowired
    private GlobalVariable globalVariable;
    public VisitingRecordItem initItemByInodeNumber(Integer number)
    {
        VisitingRecordItem visitingRecordItem = new VisitingRecordItem();
        DirNode dirNodeByNumber = dirNodeOperate.getDirNodeByNumber(number);
        visitingRecordItem.inode=globalVariable.inodeArray[number];
        visitingRecordItem.dirNode=dirNodeByNumber;
        return visitingRecordItem;
    }
    public void initRootItem()
    {
        VisitingRecordItem visitingRecordItem = new VisitingRecordItem();
        visitingRecordItem.inode=globalVariable.rootInode;
        visitingRecordItem.dirNode=globalVariable.rootDirNode;
        globalVariable.visitingRecord=new Stack<>();
        globalVariable.visitingRecord.push(visitingRecordItem);
    }
    public boolean pop()
    {
        if(globalVariable.visitingRecord.size()==1)
        {
            return false;
        }
        else if(globalVariable.visitingRecord.size()>1)
        {
            globalVariable.visitingRecord.pop();
            return true;
        }
        return false;
    }
    public boolean push(Integer number)
    {
        globalVariable.visitingRecord.push(initItemByInodeNumber(number));
        return true;
    }
}
